<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SendsmsController extends Controller
{
    //发送短信
    public function sendcode(Request $req)
    {
        switch (request('method')) {
            case 'register':
                $rule = 'required|regex:/^1[34578][0-9]{9}$/|unique:users';
                $template = 'SMS_151040148';
                break;
            case 'forgotpass':
                $rule = 'required|regex:/^1[34578][0-9]{9}$/|exists:users';
                $template = 'SMS_151040150';
                break;
            case 'tixiancode':
                $rule = 'required|regex:/^1[34578][0-9]{9}$/|exists:users';
                $template = 'SMS_151075126';
                break;
            case 'bangding':
                $rule = 'required|regex:/^1[34578][0-9]{9}$/';
                $template = 'SMS_153420154';
                break;
            default:
                return response()->json(['message' => '参数错误', 'status' => 0]);
        }
        $req->validate([
            'mobile' => $rule,
        ]);
        $code = rand(100000, 999999);
        Redis::setex($req->mobile, 300, $code);
        $aliSms = new AliSms();
        $res = $aliSms->sendSms($req->mobile, $template, ['code' => $code]);
        if ($res->Code == 'OK') {
            $data = [
                'message' => '发送成功',
                'status' => 1
            ];
            if (request('method') == 'bangding') {
                if (User::where('mobile', $req->mobile)->first()) {
                    $data['registed'] = 1;
                } else {
                    $data['registed'] = 0;
                }
            }
            return response()->json($data);
        }
        return response()->json(['message' => '发送失败', 'status' => 0]);
    }

    public function choose()
    {

    }
}
